<?php
class ChitietviphamsController extends AppController {

	var $name = 'Chitietviphams';
	var $helpers = array('Form','Html','Js');
	var $components = array('RequestHandler');
	function index() {
		$this->Chitietvipham->recursive = 0;
		$this->set('chitietviphams', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid chitietvipham', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('chitietvipham', $this->Chitietvipham->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Chitietvipham->create();
			if ($this->Chitietvipham->save($this->data)) {
				$this->Session->setFlash(__('The chitietvipham has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The chitietvipham could not be saved. Please, try again.', true));
			}
		}
		$docgias = $this->Chitietvipham->Docgia->find('list');
		$bienlaiphats = $this->Chitietvipham->Bienlaiphat->find('list');
		$hinhthucxuphats = $this->Chitietvipham->Hinhthucxuphat->find('list');
		$this->set(compact('docgias', 'bienlaiphats', 'hinhthucxuphats'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid chitietvipham', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Chitietvipham->save($this->data)) {
				$this->Session->setFlash(__('The chitietvipham has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The chitietvipham could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Chitietvipham->read(null, $id);
		}
		$docgias = $this->Chitietvipham->Docgium->find('list');
		$bienlaiphats = $this->Chitietvipham->Bienlaiphat->find('list');
		$hinhthucxuphats = $this->Chitietvipham->Hinhthucxuphat->find('list');
		$this->set(compact('docgias', 'bienlaiphats', 'hinhthucxuphats'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for chitietvipham', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Chitietvipham->delete($id)) {
			$this->Session->setFlash(__('Chitietvipham deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Chitietvipham was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

	function thongkebl($ngaybd=0,$ngaykt=0,$nam=0)
	{
		$isAjax = 0;
		if($this->RequestHandler->isAjax())
		{
			$isAjax = 1;			
			$this->Chitietvipham->recursive=2;
			if($this->data['Chitietvipham']['theo']=='ngay')
			{
				$ngaybd = date('Y-m-d',strtotime($this->data['Chitietvipham']['ngaybd']));
				if(empty($this->data['Chitietvipham']['ngaykt']))
					$ngaykt = $ngaybd;
				else
					$ngaykt = date('Y-m-d',strtotime($this->data['Chitietvipham']['ngaykt']));
				$conditions = array('Bienlaiphat.ngaylap >='=>$ngaybd,'Bienlaiphat.ngaylap <='=>$ngaykt);
			}
			else
			{
				$nam = $this->data['Chitietvipham']['nam'];
				$conditions = array('YEAR(ngaylap)'=>$nam);
			}
			$this->paginate=array('conditions'=>$conditions,'limit'=>20);
			$chitietphats = $this->paginate();
			$this->set(compact('chitietphats','ngaybd','ngaykt','nam'));
		}
		else
		{
			if($nam!=0)
			{
				$conditions = array('YEAR(ngaylap)'=>$nam);
			}
			else
			{
				$conditions = array('Bienlaiphat.ngaylap >='=>$ngaybd,'Bienlaiphat.ngaylap <='=>$ngaykt);
			}
			$this->paginate=array('conditions'=>$conditions,'limit'=>20);
			$this->Chitietvipham->recursive=2;
			$chitietphats = $this->paginate();
			$this->set(compact('chitietphats','ngaybd','ngaykt','nam'));
		}
		$this->set(compact('isAjax'));
	}
    
    function thongkexls($ngaybd=0,$ngaykt=0,$nam=0)
	{
	   $this->layout='';
		$isAjax = 0;
		if($this->RequestHandler->isAjax())
		{
			$isAjax = 1;			
			$this->Chitietvipham->recursive=2;
			if($this->data['Chitietvipham']['theo']=='ngay')
			{
				$ngaybd = date('Y-m-d',strtotime($this->data['Chitietvipham']['ngaybd']));
				if(empty($this->data['Chitietvipham']['ngaykt']))
					$ngaykt = $ngaybd;
				else
					$ngaykt = date('Y-m-d',strtotime($this->data['Chitietvipham']['ngaykt']));
				$conditions = array('Bienlaiphat.ngaylap >='=>$ngaybd,'Bienlaiphat.ngaylap <='=>$ngaykt);
			}
			else
			{
				$nam = $this->data['Chitietvipham']['nam'];
				$conditions = array('YEAR(ngaylap)'=>$nam);
			}
			//$this->paginate=array('conditions'=>$conditions,'limit'=>20);
			//$chitietphats = $this->paginate();
            $chitietphats=$this->Chitietvipham->find('all',array('conditions'=>$conditions));
			$this->set(compact('chitietphats','ngaybd','ngaykt','nam'));
		}
		else
		{
			if($nam!=0)
			{
				$conditions = array('YEAR(ngaylap)'=>$nam);
			}
			else
			{
				$conditions = array('Bienlaiphat.ngaylap >='=>$ngaybd,'Bienlaiphat.ngaylap <='=>$ngaykt);
			}
			//$this->paginate=array('conditions'=>$conditions,'limit'=>20);
			$this->Chitietvipham->recursive=2;
			//$chitietphats = $this->paginate();
            $chitietphats=$this->Chitietvipham->find('all',array('conditions'=>$conditions));
			$this->set(compact('chitietphats','ngaybd','ngaykt','nam'));
		}
		$this->set(compact('isAjax'));
	}
}
